package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import server.HTTPServer;
import shared.Contacts;
import shared.MyData;
import shared.User;
//test
public class HTTPClient {
	User me;
	int timeout=4000;
	int ttl=4000;
	
	public HTTPClient() {
		//this.me = me;
	}
	
	public void send(String message, String To) throws IOException{
		
		Socket echoSocket = new Socket();
		Socket clientSocket = null;
		Contacts contacts=new Contacts();
		User user=contacts.getContact(To);
		
		String ip=user.getIP();
		String port=user.getPort();
		try
		{
			clientSocket = new Socket();
	        InetSocketAddress socketAddress = new InetSocketAddress(ip, Integer.parseInt(port));
	        clientSocket.connect(socketAddress,timeout);
			echoSocket.connect(socketAddress, timeout);
			PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);
			outToServer.println( "/chat/sendmessage?myname=" + MyData.getName() + "&myip=" + MyData.getIP() + ":" + MyData.getPort() + "&message=" + message + "&ttl=0");
		
		}
		catch(IOException o)
		{
			System.out.println("Error writting to the socket: " + o);
			return;

		}finally {
            try {
                // close socket
            	clientSocket.close();
            } catch (IOException ex) {
                System.err.println("SendMessage socket close error");
            }  
        }
	}
	public void sendName(String name, String IpPort){
		
		Socket echoSocket = new Socket();
		Socket clientSocket = null;
		try
		{
			clientSocket = new Socket();
			String[] ip=IpPort.split(":");
			InetSocketAddress socketAddress = new InetSocketAddress(ip[0], Integer.parseInt(ip[1]));
			clientSocket.connect(socketAddress, timeout);
			PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);
			outToServer.println( "/chat/sendname?name=" + name + "&myip=" + MyData.IP + ":" + MyData.getPort() + "&ttl=" + ttl);
		
		}
		catch(IOException o)
		{
			System.out.println("Error writting to the socket: " + o);
			return;

		}finally {
            try {
                // close socket
            	clientSocket.close();
            } catch (IOException ex) {
                System.err.println("SendMessage socket close error");
            }  
        }	
		
		
	}
	public void findName(String searchName, String IpPort,String addedPort) {
		Socket clientSocket = null;
		try {
			clientSocket = new Socket();
			String[] ip=IpPort.split(":");
			String[] addedIp=addedPort.split(":");
			InetSocketAddress socketAddress = new InetSocketAddress(ip[0], Integer.parseInt(ip[1]));
			clientSocket.connect(socketAddress, timeout);
			PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);
			outToServer.println( "/chat/findname?name=" + searchName + "&myip=" + addedIp[0] + ":" + addedIp[1] + "&ttl=" + ttl);
			clientSocket.close();
		}
		catch (IOException e) {
		} finally {
            try {
                // close socket
            	clientSocket.close();
            } catch (IOException ex) {
            }  
        }
	}
	
	/*
	public void askNames(String name){
		Socket clientSocket = null;
		Contacts contacts=new Contacts();
		User user=contacts.getContact(name);
		try {
			InetSocketAddress socketAddress = new InetSocketAddress(user.getIP(), Integer.parseInt(user.getPort()));
			clientSocket.connect(socketAddress, timeout);
			PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);     
			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

			outToServer.println(HTTPRequest( "/chat/asknames?ttl=0" ));
		}
		catch (IOException e) {
		} finally {
            try {
                // close socket
            	clientSocket.close();
            } catch (IOException ex) {
            }  
        }
	}
	
    */
	
	public void getMessage(){
		
	}
	
	public void addUser(String name){
		
	}
}